# coding:utf-8
import sys
import os
import buildMyDictionary as bmd

"""
able to input path 
the dir path  should be empty for it will be the root of your dictionary
eg. new dic "python setup.py -new ./myDictionary"
    exist dic "python setup.py -load ./myDictionary"
"""
NewOrLoad = {
    "-new": 1,
    "-load": 0
}

inputed = sys.argv


def main():
    if len(sys.argv) == 3:
        doType = NewOrLoad[inputed[1]]
        fpath = inputed[-1]
    else:
        print("you can also create your dictionary by input as:\n "
              "'python setup.py -new ./myDictionary'\n"
              " or load existing dictionary as:\n "
              "'python setup.py -load ./myDictionary'")
        fpath = input("请输入个人字典文件夹路径：\n")
        if os.path.exists(fpath):
            fdirs = os.listdir(fpath)
            if len(fdirs) and "MaDicStateLog.csv" not in fdirs:
                print(f"{fpath} 不是一个空文件夹,且没有'MaDicStateLog.csv',无法进行新建、读取后续操作\n"
                      f"请检查后重新输入")
                ex = input("退出请输入'q',任意键重启程序:")
                if ex.lower() == "q":
                    sys.exit()
                else:
                    main()
                    sys.exit()
        wishNew = input("您是否要在此文件夹下初始化字典[y/n]（回车默认否）：")
        if not len(wishNew) or wishNew.lower() == "n":
            doType = 0
        elif wishNew.lower() == "y":
            doType = 1
        else:
            print(f"no {wishNew} command")
            sys.exit()

    if doType == 0:
        md = bmd.MyDictionary(fpath)
    elif doType == 1:
        newName = os.path.basename(fpath.strip("\/"))
        md = bmd.MyDictionary(os.path.join(fpath, "../"), newName)


if __name__ == '__main__':
    main()
    pass
